package com.mmall.controller.backend;

import com.github.pagehelper.PageInfo;
import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.dto.PageDto;
import com.mmall.pojo.Article;
import com.mmall.pojo.User;
import com.mmall.service.IArticleService;
import com.mmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @CLASS ArticleManageController
 * @DESCRIPTION TODO
 * @AUTHor tang
 * @Date 2019-08-23 10:17
 * @VERSION 1.0
 **/
@Controller
@RequestMapping("/manage/article")
public class ArticleManageController {
    @Autowired
    private IUserService iUserService;

    @Autowired
    private IArticleService iArticleService;

    @RequestMapping(value = "list",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<PageInfo> list(HttpSession session,
                                         @RequestBody PageDto pageDto){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录请重新登录");
        }
        ServerResponse response = iUserService.checkAdminRole(user);
        if (!response.isSuccess()){
            return ServerResponse.createByErrorMessage("当前用户没有登录后台权限");
        }else {
            return iArticleService.list(user.getId(),pageDto.getPageNum(),pageDto.getPageSize());
        }
    }

    @RequestMapping(value = "updateSave",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> updateSave(HttpSession session, @RequestBody Article article){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录请重新登录");
        }
        ServerResponse response = iUserService.checkAdminRole(user);
        if (!response.isSuccess()){
            return ServerResponse.createByErrorMessage("当前用户没有登录后台权限");
        }else {
            return iArticleService.updateSave(user.getId(),user.getUsername(),article);
        }
    }

    @RequestMapping(value = "delete",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<String> delete(HttpSession session, Integer articleId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录请重新登录");
        }
        ServerResponse response = iUserService.checkAdminRole(user);
        if (!response.isSuccess()){
            return ServerResponse.createByErrorMessage("当前用户没有登录后台权限");
        }else {
            return iArticleService.delete(user.getId(),articleId);
        }
    }

    @RequestMapping(value = "detail",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<Article> detail(HttpSession session, Integer articleId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录请重新登录");
        }
        ServerResponse response = iUserService.checkAdminRole(user);
        if (!response.isSuccess()){
            return ServerResponse.createByErrorMessage("当前用户没有登录后台权限");
        }else {
            return iArticleService.detail(user.getId(),articleId);
        }
    }


}
